import { MES_SMH_ApiHost } from '@/utils/Interceptors'

// 装配工单数据类型 - 根据Swagger文档更新
export interface AssemblyWorkOrder {
  assemblyWorkOrderId: number
  documentNo: string
  workOrderCount: number
  finishedWorkOrders: number
  documentProgress: string
  remark: string
  createTime: string
  creator: string
  modifier: string
  modifyTime: string
}

// 装配工单产品明细数据类型 - 根据Swagger文档更新
export interface AssemblyWorkOrderDetail {
  assemblyWorkOrderDetailId: number
  assemblyWorkOrderId: number
  levelNumber: string
  productCode: string
  productName: string
  productSpec: string
  workOrderNo: string
  quantity: number
  completedQty: number
  createTime: string
  creator: string
  modifier: string
  modifyTime: string
}

// API响应类型
export interface ApiResponse<T> {
  code: number
  msg: string
  data: T
}

// 分页响应类型 - 根据Swagger文档更新
export interface PagedResponse<T> {
  totalCount: number
  totalPage: number
  pageData: T[]
}

// 搜索参数类型 - 根据Swagger文档更新
export interface SearchParams {
  documentNo?: string
  workOrderCount?: number
  finishedWorkOrders?: number
  pageIndex?: number
  pageSize?: number
}

// 创建装配工单明细类型
export interface CreateAssemblyWorkOrderDetail {
  productCode: string
  productName?: string
  productSpec?: string
  workOrderNo?: string
  quantity?: number
  levelNumber?: string
  completedQty?: number
}

// 创建装配工单（包含明细）类型
export interface CreateAssemblyWorkOrderWithDetails {
  assemblyWorkOrder: {
    documentNo: string
    workOrderCount?: number
    finishedWorkOrders?: number
    documentProgress?: string
    remark?: string
    creator?: string
  }
  details?: CreateAssemblyWorkOrderDetail[]
}

// 获取装配工单分页列表 - 根据Swagger文档更新
export const getAssemblyWorkOrders = async (params: SearchParams) => {
  return await MES_SMH_ApiHost.post<ApiResponse<PagedResponse<AssemblyWorkOrder>>>('/api/AssemblyWorkOrder/GetPagedList', params)
}

// 获取装配工单产品明细
export const getAssemblyWorkOrderDetails = (assemblyWorkOrderId: number) => {
  return MES_SMH_ApiHost.get<ApiResponse<AssemblyWorkOrderDetail[]>>(`/api/AssemblyWorkOrder/GetProductDetails/${assemblyWorkOrderId}`)
}

// 创建装配工单
export const createAssemblyWorkOrder = async (data: Partial<AssemblyWorkOrder>) => {
  return await MES_SMH_ApiHost.post<ApiResponse<AssemblyWorkOrder>>('/api/AssemblyWorkOrder/Create', data)
}

// 创建装配工单（包含明细）
export const createAssemblyWorkOrderWithDetails = async (data: CreateAssemblyWorkOrderWithDetails) => {
  return await MES_SMH_ApiHost.post<ApiResponse<AssemblyWorkOrder>>('/api/AssemblyWorkOrder/CreateWithDetails', data)
}

// 创建单个装配工单明细
export const createAssemblyWorkOrderDetail = async (assemblyWorkOrderId: number, detail: CreateAssemblyWorkOrderDetail) => {
  return await MES_SMH_ApiHost.post<ApiResponse<AssemblyWorkOrderDetail>>(`/api/AssemblyWorkOrder/CreateDetail/${assemblyWorkOrderId}`, detail)
}

// 批量创建装配工单明细
export const batchCreateAssemblyWorkOrderDetails = async (assemblyWorkOrderId: number, details: CreateAssemblyWorkOrderDetail[]) => {
  return await MES_SMH_ApiHost.post<ApiResponse<AssemblyWorkOrderDetail[]>>(`/api/AssemblyWorkOrder/BatchCreateDetails/${assemblyWorkOrderId}`, details)
}

// 更新装配工单
export const updateAssemblyWorkOrder = async (id: number, data: Partial<AssemblyWorkOrder>) => {
  return await MES_SMH_ApiHost.put<ApiResponse<AssemblyWorkOrder>>(`/api/AssemblyWorkOrder/UpdateAssemblyWorkOrder/${id}`, data)
}

// 删除装配工单
export const deleteAssemblyWorkOrder = async (id: number) => {
  return await MES_SMH_ApiHost.delete<ApiResponse<boolean>>(`/api/AssemblyWorkOrder/DeleteAssemblyWorkOrder/${id}`)
}

// 批量删除装配工单
export const batchDeleteAssemblyWorkOrders = async (ids: number[]) => {
  return await MES_SMH_ApiHost.post<ApiResponse<boolean>>('/api/AssemblyWorkOrder/BatchDeleteAssemblyWorkOrders', { ids })
}

// 导出装配工单模板
export const exportAssemblyWorkOrderTemplate = async () => {
  return await MES_SMH_ApiHost.get('/api/AssemblyWorkOrder/ExportTemplate', { 
    responseType: 'blob',
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, */*'
    }
  })
}

// 导出装配工单数据
export const exportAssemblyWorkOrders = async (params?: SearchParams) => {
  return await MES_SMH_ApiHost.get('/api/AssemblyWorkOrder/ExportAssemblyWorkOrders', { 
    params,
    responseType: 'blob',
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, */*'
    }
  })
}
